package com.atguigu.gmall.gmallsugar.controller;

import bean.C3Stats;
import bean.TmStats;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.gmallsugar.bean.KeywordStats;
import com.atguigu.gmall.gmallsugar.bean.ProvinceStats;
import com.atguigu.gmall.gmallsugar.bean.VisitorStats;
import com.atguigu.gmall.gmallsugar.service.KeywordService;
import com.atguigu.gmall.gmallsugar.service.ProductService;
import com.atguigu.gmall.gmallsugar.service.ProvinceService;
import com.atguigu.gmall.gmallsugar.service.VisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/8/9 9:58
 */
@RestController
public class SugarController {
    
    @Autowired
    ProductService product;
    
    @RequestMapping("/sugar/api/gmv")
    public String gmv(@RequestParam(value = "date", defaultValue = "0") int date) {
        if (date == 0) {
            //            没有传入日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
            
        }
        
        BigDecimal gmv = product.getGMV(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        result.put("data", gmv);
        
        return result.toJSONString();
        
    }
    
    @RequestMapping("/sugar/api/gmv/tm")
    public String gmvTm(
        @RequestParam(value = "date", defaultValue = "0") int date,
        @RequestParam(value = "limit", defaultValue = "5") int limit) {
        if (date == 0) {
            //            没有传入日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
            
        }
        
        List<TmStats> list = product.getGMVByTM(date, limit);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        JSONArray categories = new JSONArray();
        // TODO
        for (TmStats tmStats : list) {
            categories.add(tmStats.getTm_name());
        }
        data.put("categories", categories);
        
        JSONArray series = new JSONArray();
        //TODO
        data.put("series", series);
        
        JSONObject obj = new JSONObject();
        obj.put("name", "商品品牌");
        JSONArray data1 = new JSONArray();
        for (TmStats tmStats : list) {
            data1.add(tmStats.getOrder_amount());
        }
        obj.put("data", data1);
        
        series.add(obj);
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    @RequestMapping("/sugar/api/gmv/c3")
    public String gmvC3(
        @RequestParam(value = "date", defaultValue = "0") int date,
        @RequestParam(value = "limit", defaultValue = "5") int limit) {
        if (date == 0) {
            //            没有传入日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
            
        }
        
        List<C3Stats> list = product.getGMVByC3(date, limit);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONArray data = new JSONArray();
        for (C3Stats c3Stats : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", c3Stats.getCategory3_name());
            obj.put("value", c3Stats.getOrder_amount());
            
            data.add(obj);
        }
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    @RequestMapping("/sugar/api/gmv/spu")
    public String gmvSpu(
        @RequestParam(value = "date", defaultValue = "0") int date,
        @RequestParam(value = "limit", defaultValue = "50") int limit) {
        if (date == 0) {
            //            没有传入日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
            
        }
        List<Map<String, Object>> list = product.getGMVBySpu(date, limit);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray columns = new JSONArray();
        JSONObject c1 = new JSONObject();
        c1.put("name", "spu名字");
        c1.put("id", "spu_name");
        columns.add(c1);
        
        JSONObject c2 = new JSONObject();
        c2.put("name", "销售额");
        c2.put("id", "order_amount");
        columns.add(c2);
        
        data.put("columns", columns);
        
        JSONArray rows = new JSONArray();
        for (Map<String, Object> map : list) {
            
            JSONObject row = new JSONObject();
            row.put("spu_name", map.get("spu_name"));
            row.put("order_amount", map.get("order_amount"));
            
            rows.add(row);
        }
        data.put("rows", rows);
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    @Autowired
    ProvinceService province;
    
    @RequestMapping("/sugar/api/province")
    public String provinceMap(
        @RequestParam(value = "date", defaultValue = "0") int date) {
        if (date == 0) {
            //            没有传入日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
            
        }
        
        List<ProvinceStats> list = province.getGMVByProvince(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        JSONArray mapData = new JSONArray();
        // TODO
        for (ProvinceStats ps : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", ps.getProvince_name());
            obj.put("value", ps.getOrder_amount());
            
            JSONArray tooltipValues = new JSONArray();
            tooltipValues.add(ps.getOrder_count());
            obj.put("tooltipValues", tooltipValues);
            
            mapData.add(obj);
        }
        
        data.put("mapData", mapData);
        
        data.put("valueName", "销售额");
        
        JSONArray tooltipNames = new JSONArray();
        tooltipNames.add("订单个数");
        data.put("tooltipNames", tooltipNames);
        
        JSONArray tooltipUnits = new JSONArray();
        tooltipUnits.add("个");
        data.put("tooltipUnits", tooltipUnits);
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    @Autowired
    VisitorService visitor;
    
    @RequestMapping("/sugar/api/visitor/hour")
    public String visitorByHour(
        @RequestParam(value = "date", defaultValue = "0") int date) {
        if (date == 0) {
            //            没有传入日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
            
        }
        
        List<VisitorStats> list = visitor.statsByHour(date);
        HashMap<Integer, VisitorStats> hourToVS = new HashMap<>(); // 0 -> vs, 1->vs
        for (VisitorStats vs : list) {
            hourToVS.put(Integer.parseInt(vs.getHour()), vs);
        }
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray categories = new JSONArray();
        for (int i = 0; i < 24; i++) {
            // 00 01 02 03 ... 12 13 14
            categories.add(new DecimalFormat("00").format(i));
        }
        
        data.put("categories", categories);
        
        JSONArray series = new JSONArray();
        
        JSONObject pv = new JSONObject();
        pv.put("name", "pv");
        JSONArray pvData = new JSONArray();
        for (int i = 0; i < 23; i++) {
            VisitorStats vs = hourToVS.get(i);
            if (vs != null) {
                pvData.add(vs.getPv_ct());
            } else {
                pvData.add(0);
            }
        }
        pv.put("data", pvData);
        series.add(pv);
        
        JSONObject uv = new JSONObject();
        uv.put("name", "uv");
        JSONArray uvData = new JSONArray();
        for (int i = 0; i < 23; i++) {
            VisitorStats vs = hourToVS.get(i);
            if (vs != null) {
                uvData.add(vs.getUv_ct());
            } else {
                uvData.add(0);
            }
        }
        uv.put("data", uvData);
        series.add(uv);
        
        JSONObject uj = new JSONObject();
        uj.put("name", "uj");
        JSONArray ujData = new JSONArray();
        for (int i = 0; i < 23; i++) {
            VisitorStats vs = hourToVS.get(i);
            if (vs != null) {
                ujData.add(vs.getUj_ct());
            } else {
                ujData.add(0);
            }
        }
        uj.put("data", ujData);
        series.add(uj);
        
        data.put("series", series);
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    @RequestMapping("/sugar/api/visitor/isnew")
    public String visitorByIsNew(
        @RequestParam(value = "date", defaultValue = "0") int date) {
        if (date == 0) {
            //            没有传入日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
            
        }
        
        List<VisitorStats> list = visitor.statsByIsNew(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        data.put("total", list.size());
        
        JSONArray columns = new JSONArray();
        
        JSONObject c1 = new JSONObject();
        c1.put("name", "新老用户(1新0旧)");
        c1.put("id", "is_new");
        columns.add(c1);
        
        JSONObject c2 = new JSONObject();
        c2.put("name", "pv");
        c2.put("id", "pv");
        columns.add(c2);
        
        JSONObject c3 = new JSONObject();
        c3.put("name", "uv");
        c3.put("id", "uv");
        columns.add(c3);
        JSONObject c4 = new JSONObject();
        c4.put("name", "uj");
        c4.put("id", "uj");
        columns.add(c4);
        
        data.put("columns", columns);
        
        JSONArray rows = new JSONArray();
        for (VisitorStats vs : list) {
            JSONObject row = new JSONObject();
            row.put("is_new", vs.getIs_new());
            row.put("pv", vs.getPv_ct());
            row.put("uv", vs.getUv_ct());
            row.put("uj", vs.getUj_ct());
            
            rows.add(row);
        }
        data.put("rows", rows);
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    
    @Autowired
    KeywordService kw;
    @RequestMapping("/sugar/api/kw")
    public String kw(@RequestParam(value = "date", defaultValue = "0") int date,
                     @RequestParam(value = "limit", defaultValue = "15") int limit) {
        if (date == 0) {
            //            没有传入日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
            
        }
    
        List<KeywordStats> list = kw.statsKeyword(date, limit);
    
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
    
        JSONArray data = new JSONArray();
    
        for (KeywordStats ks : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", ks.getKeyword());
            obj.put("value", ks.getScore());
    
            data.add(obj);
        }
    
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
}
